package com.xkdx.guangguang.fragment.user;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.xkdx.guangguang.fragment.user.UserRegistModule.DetailInfo;
import com.xkdx.guangguang.fragment.user.UserRegistModule.User;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;

public class UserLoginModule extends AbsModule {

	public User user;
	private DetailInfo info;
	JSONObject values;
	private Context context;
	private UserSharePrefence user_sp;
	JSONArray jsonArray;
	JSONObject jsonObj;

	public UserLoginModule(Context context) {
		this.context = context;
	}

	@Override
	public void parseData() throws Exception {

		try {
			user = new UserRegistModule(context).new User();
			info = new UserRegistModule(context).new DetailInfo();
			jsonArray = (JSONArray) result;
			jsonObj = (JSONObject) jsonArray.get(0);

			user = parserUserRgist(jsonObj.getJSONObject("Content"));

		} catch (Exception e) {

			throw e;
		}

	}

	public User parserUserRgist(JSONObject userLogin) throws Exception {

		try {
			JSONObject stut = (JSONObject) userLogin.get("Result");

			user.setErrorCode(stut.getString("ErrorCode"));
			user.setMessage(stut.getString("Message"));
			user.setType(stut.getString("Type"));
			user.setStatus(stut.getString("Status"));

			if (stut.has("DetailInfo")) {
				JSONObject detail = (JSONObject) stut
						.getJSONObject("DetailInfo");
				info.setSex(detail.getString("Sex"));
				info.setBirthday(detail.getString("Birthday"));
				info.setEmail(detail.getString("Email"));
				info.setEmailVerified(detail.getString("EmailVerified"));
				info.setMobilePhone(detail.getString("MobilePhone"));
				info.setMobilePhoneVerified(detail
						.getString("MobilePhoneVerified"));
				info.setUserID(detail.getString("UserID"));
				info.setLoginToken(detail.getString("LoginToken"));
				info.setNickName(detail.getString("NickName"));
				info.setPictureUrl(detail.getString("PictureUrl"));
				info.setSinaWeiboNickName(detail.getString("SinaWeiboNickName"));
				info.setSinaWeiboToken(detail.getString("SinaWeiboToken"));
				info.setSinaWeiboExpireTime(detail
						.getString("SinaWeiboExpireTime"));
				info.setPoint(detail.getString("Point"));
				info.setSinaWeiboUserID(detail.getString("SinaWeiboUserID"));
				info.setQQOpenID(detail.getString("QQOpenID"));
				info.setQQNickName(detail.getString("QQNickName"));
				info.setQQToken(detail.getString("QQToken"));
				info.setQQExpireTime(detail.getString("QQExpireTime"));
				info.setVerifyType(detail.getString("UserType"));
				if (detail.getString("ShopIds") != null
						&& !("".equals(detail.getString("ShopIds")))) {
					info.setShopIds(detail.getString("ShopIds"));
				} else {
					info.setShopIds("");
				}
				user.setDetailInfo(info);
				// 将用户信息存到本地
				if ("Success".equals(stut.getString("Status"))) {
					user_sp = new UserSharePrefence(context, IConstants.SP_USER);
					user_sp.setUserBirthday(detail.getString("Birthday"));
					user_sp.setUserEmail(detail.getString("Email"));
					user_sp.setUserEmailVerified(detail
							.getString("EmailVerified"));
					user_sp.setUserSex(detail.getString("Sex"));
					user_sp.setUserID(detail.getString("UserID"));
					user_sp.setUserMessage(stut.getString("Message"));
					user_sp.setUserStatus(stut.getString("Status"));
					user_sp.setUserMobilePhone(detail.getString("MobilePhone"));
					user_sp.setUserMobilePhoneVerified(detail
							.getString("MobilePhoneVerified"));
					user_sp.setUserErrorCode(stut.getString("ErrorCode"));
					user_sp.setUserType(stut.getString("Type"));
					user_sp.setUserNickName(detail.getString("NickName"));
					user_sp.setUserPictureUrl(detail.getString("PictureUrl"));
					// 设置令牌
					user_sp.setUserLoginToken(detail.getString("LoginToken"));
					user_sp.setSinaWeiboUserID(detail
							.getString("SinaWeiboUserID"));
					user_sp.setSinaWeiboNickName(detail
							.getString("SinaWeiboNickName"));
					user_sp.setSinaWeiboToken(detail
							.getString("SinaWeiboToken"));
					user_sp.setSinaWeiboExpireTime(detail
							.getString("SinaWeiboExpireTime"));

					user_sp.setQQOpenID(detail.getString("QQOpenID"));
					user_sp.setQQNickName(detail.getString("QQNickName"));
					user_sp.setQQToken(detail.getString("QQToken"));
					user_sp.setQQExpireTime(detail.getString("QQExpireTime"));
					user_sp.setVerifyType(detail.getString("UserType"));
					user_sp.setUserPoint(detail.getString("Point"));
					if (detail.getString("ShopID") != null
							&& !("".equals(detail.getString("ShopID")))) {
						user_sp.setShopIds(detail.getString("ShopID"));
					} else {
						user_sp.setShopIds("");
					}
				}
			}
		} catch (JSONException e) {
			throw e;
		}

		return user;

	}

}
